<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class CateController extends Controller
{
    public function add(){
    	$title ="分类添加";
    	//查询数据
    	$data = DB::table('cate') ->select('*',DB::raw("concat(path,',',id)as sort_str"))->orderby('sort_str','asc')-> get();
    	//准备缩进
    	foreach($data as $k=>$v){
    		$arr = explode(',',$v -> path);
    		$num = count($arr);
    		$str = str_repeat(' -- ',$num);
    		$data[$k]->name = $str.$v -> name;
    	}
    	return view('admin.cate.add',['title' => $title,'data' => $data]);
    }
    public function insert(Request $request)
    {
    	// dd($request ->all());
    	$data = $request ->except('_token');
        // dd($data);
    	//处理path
        if($data['pid'] !=0)
        {
    	$parent_path =  DB::table('cate')->where('id','=',$data['pid'])->first()->path;
        $data['path'] = $parent_path.','.$data['pid'];
        }else{$parent_path = 0;
            $data['path'] = $data['pid'];
        }
    	

    	//处理status
    	 
    	$res = DB::table('cate') ->insert($data);
    	if($res)
    	{
    		return redirect('/admin/cate/index')->with(['info'=>'添加成功']);
    	}
    	//index


    }

    public function index()
    	{
    		$title = '分类列表';
    		$data = DB::table('cate AS c1')
    		-> leftJoin("cate AS c2",'c1.pid','=','c2.id')
    		->select('c1.*','c2.name AS p_name',DB::raw("CONCAT(c1.path,',',c1.id)AS sort_str"))
    		->orderBy('sort_str','ASC')


    		->get();

    		foreach($data as $k =>$v){
    			$arr = explode(',',$v -> path);
	    		$num = count($arr);
	    		$str = str_repeat(' -- ',$num);
	    		$data[$k]->name = $str.$v -> name;
	    		if(empty($data[$k] ->pid))
	    		{
	    			$data[$k] -> p_name = '--';
	    		}
    		}

    		return view('admin.cate.index',['title'=>$title,'data'=>$data]);
    	}
    	//分类edit
    	public function edit($id)
    	{
    		$title = '分类编辑';
    		$data = DB::table('cate') ->where('id',$id) -> first();

    		//查询所有分类
    		$allData = DB::table('cate') ->select('*',DB::raw("concat(path,',',id)as sort_str"))->orderby('sort_str','asc')-> get();
	    	//准备缩进
	    	foreach($allData as $k=>$v){
	    		$arr = explode(',',$v -> path);
	    		$num = count($arr);
	    		$str = str_repeat(' -- ',$num);
	    		$allData[$k]->name = $str.$v -> name;
	    	}
    		





    		return view('admin.cate.edit',['title'=>$title,'data'=>$data,'allData'=>$allData]);


    	}
    	//update
    	public function update(Request $request)
    	{
    		$data = $request ->except('_token','id');
    		//处理path
    		$parent_path = DB::table('cate') -> where('id','=',$data['pid']) -> first() -> path;
    		//echo $path;
    		$data['path'] = $parent_path.','.$data['pid'];
    		//更新
    		$res = DB::table('cate') -> where('id',$request -> input('id')) -> update($data);
    		if($res)
	    	{
	    		return redirect('/admin/cate/index')->with(['info'=>'添加成功']);
	    	}
    	}

    	//delete
    	public function delete($id)
    	{
    		$data = DB::table('cate') -> where('pid','=',$id) -> get();
    		if($data)
    		{
    			return redirect('/admin/cate/index') -> with(['info' =>'对不起，有娃呢！']);
    		}else
    		{
    			DB::table('cate')-> where('id',$id) -> delete();
    			return back() -> with(['info'=>'删除成功']);
    		}
    	}


}
